<?php
session_start();
if (empty($_SESSION['username'])) {
    echo '<script type="text/javascript">window.location.href="../index.php";</script>';
} else {
    $now = time();
    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo '<script type="text/javascript">var r=confirm("Session expire (30 mins)!"); if(r==true){window.location.href="../index.php";}else{window.location.href="index.php";}</script>';
    } else {
        require '../model-db-connection/config.php';
        $config = require '../model-db-connection/qrc_conf.properties.php';
    }
}
?>
<!DOCTYPE html>
<html lang="en">

    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>QRC - Building Management</title>
        <link rel="icon" type="image/ico" href="../images/favicon.ico" />
        <!-- Bootstrap Core CSS -->
        <link href="../css/bootstrap.min.css" rel="stylesheet">
        <link href="../css/jquery.toastmessage.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="../css/plugins/metisMenu/metisMenu.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="../css/sb-admin-2.css" rel="stylesheet">
        <!-- Custom Fonts -->
        <link href="../font-awesome-4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
        <link rel="stylesheet" href="../css/minoral.css">
        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
        <style type="text/css">
            .spinner {
                width: 70px;
                text-align: center;
            }
            #spinnerCE {
                width: 70px;
                text-align: center;
            }
            #spinnerCE > div {
                width: 18px;
                height: 18px;
                background-color: #333;

                border-radius: 100%;
                display: inline-block;
                -webkit-animation: bouncedelay 1.4s infinite ease-in-out;
                animation: bouncedelay 1.4s infinite ease-in-out;
                /* Prevent first frame from flickering when animation starts */
                -webkit-animation-fill-mode: both;
                animation-fill-mode: both;
            }
            .spinner > div {
                width: 18px;
                height: 18px;
                background-color: #333;

                border-radius: 100%;
                display: inline-block;
                -webkit-animation: bouncedelay 1.4s infinite ease-in-out;
                animation: bouncedelay 1.4s infinite ease-in-out;
                /* Prevent first frame from flickering when animation starts */
                -webkit-animation-fill-mode: both;
                animation-fill-mode: both;
            }
            #spinnerCE .bounce1 {
                -webkit-animation-delay: -0.32s;
                animation-delay: -0.32s;
            }

            #spinnerCE .bounce2 {
                -webkit-animation-delay: -0.16s;
                animation-delay: -0.16s;
            }
            .spinner .bounce1 {
                -webkit-animation-delay: -0.32s;
                animation-delay: -0.32s;
            }

            .spinner .bounce2 {
                -webkit-animation-delay: -0.16s;
                animation-delay: -0.16s;
            }
            @-webkit-keyframes bouncedelay {
                0%, 80%, 100% { -webkit-transform: scale(0.0) }
                40% { -webkit-transform: scale(1.0) }
            }

            @keyframes bouncedelay {
                0%, 80%, 100% { 
                    transform: scale(0.0);
                    -webkit-transform: scale(0.0);
                } 40% { 
                    transform: scale(1.0);
                    -webkit-transform: scale(1.0);
                }
            }

        </style>
    </head>

    <body>

        <div id="wrapper">

            <!-- Navigation -->
            <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="index-dashboard.php"><img src="../images/qrc_logo.JPG" height="30"/> QRC-Building Management</a>
                </div>
                <!-- /.navbar-header -->

                <ul class="nav navbar-top-links navbar-right">
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                            <i class="fa fa-user fa-fw"></i> <?= $_SESSION['username']; ?> <i class="fa fa-caret-down"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>
                            </li>
                            <li><a href="../setting-page/index-setting.php"><i class="fa fa-gear fa-fw"></i> Settings</a>
                            </li>
                            <li class="divider"></li>
                            <li><a href="#" id="logout_click"><i class="fa fa-sign-out fa-fw"></i> Logout</a>
                            </li>
                        </ul>
                        <!-- /.dropdown-user -->
                    </li>
                    <!-- /.dropdown -->
                </ul>
                <!-- /.navbar-top-links -->

                <div class="navbar-default sidebar" role="navigation">
                    <div class="sidebar-nav navbar-collapse">
                        <div id="left-menu-load"></div>
                    </div>
                    <!-- /.sidebar-collapse -->
                </div>
                <!-- /.navbar-static-side -->
            </nav>

            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">Team Builder (ทีม)</h1>
                        <ol class="breadcrumb">
                            <li class="active">
                                <i class="fa fa-steam-square"></i> Team (ทีม)
                            </li>
                        </ol>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                <!-- /.row -->
                <div class="row" id="load_all_status">

                </div>
                <!-- /.row -->
                <div class="row">
                    <div class="col-lg-10" id="create_edit_panel">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <i class="fa fa-bar-chart-o fa-fw"></i> Create/Edit Team Builder (สร้าง/แก้ไข ทีม)
                                <div class="pull-right">
                                    <button type="button" id="save_create_panel" class="btn btn-primary btn-xs" style="width: 100px" data-toggle="dropdown">บันทึก</button>
                                    <button type="button" id="close_create_panel" class="btn btn-default btn-xs" style="width: 100px" data-toggle="dropdown">ปิด</button>
                                </div>
                            </div>
                            <!-- /.panel-heading -->
                            <div class="panel-body table-responsive" >
                                <div id="loading_ce_form" >
                                    <div id="spinnerCE">
                                        <div class="bounce1"></div>
                                        <div class="bounce2"></div>
                                        <div class="bounce3"></div>
                                    </div>

                                </div>
                            </div>
                            <!-- /.panel-body -->
                        </div>
                        <!-- /.panel -->
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-10" id="search_panel">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <i class="fa fa-bar-chart-o fa-fw"></i> Search Projects (ค้นหาโครงการ)
                                <div class="pull-right">
                                    <button type="button" id="search_team_re_btn" class="btn btn-primary btn-xs" style="width: 100px" data-toggle="dropdown">ค้นหา</button>
                                    <button type="button" id="close_search_panel" class="btn btn-default btn-xs" style="width: 100px" data-toggle="dropdown">ปิด</button>
                                </div>
                            </div>
                            <!-- /.panel-heading -->
                            <div class="panel-body" >
                                <div id="loading_search_form" class="table-responsive" >
                                    <div id="spinnerCE">
                                        <div class="bounce1"></div>
                                        <div class="bounce2"></div>
                                        <div class="bounce3"></div>
                                    </div>

                                </div>
                            </div>
                            <!-- /.panel-body -->
                        </div>
                        <!-- /.panel -->
                    </div>
                </div>
                <div class="row" id="team_tbl_content">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <i class="fa fa-bar-chart-o fa-fw"></i> All Team (ทีมช่างทั้งหมด)

                                <div class="pull-right">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                                            Actions
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu pull-right" role="menu">
                                            <li><a href="#" id="create_new_team_btn">Create New Team (สร้างทีมช่างใหม่)</a>
                                            </li>
                                            <li><a href="#" id="search_team_btn">Search Team (ค้นหาทีม)</a>
                                            </li>
                                        </ul>                                        
                                    </div>

                                </div>
                            </div>
                            <!-- /.panel-heading -->
                            <div class="panel-body">
                                <div class="table-responsive" id="loading_project">
                                    <div class="spinner">
                                        <div class="bounce1"></div>
                                        <div class="bounce2"></div>
                                        <div class="bounce3"></div>
                                    </div>
                                </div>
                            </div>
                            <!-- /.panel-body -->
                        </div>
                        <!-- /.panel -->
                    </div>
                </div>
            </div>
            <!-- /.row -->
        </div>
        <!-- /#page-wrapper -->


        <!-- jQuery Version 1.11.0 -->
        <script src="../js/jquery-1.11.0.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="../js/bootstrap.min.js"></script>

        <!-- Metis Menu Plugin JavaScript -->
        <script src="../js/plugins/metisMenu/metisMenu.min.js"></script>
        <script src="../js/jquery.toastmessage.js"></script>

        <!-- Custom Theme JavaScript -->
        <script src="../js/sb-admin-2.js"></script>
        <link rel="stylesheet" type="text/css" href="../css/jquery-ui-1.10.4.custom.css" />
        <script type="text/javascript" src="../js/jquery-ui-1.10.4.custom.js"></script>
        <script type="text/javascript" src="../js/jquery.multiselect.js"></script>
        <link rel="stylesheet" type="text/css" href="../css/jquery.multiselect.css" />
        <script>
            var createOrEditStateTeam = "Create";
            $(document).ready(function() {
                $("#search_panel").hide();
                $("#create_edit_panel").hide();
                updateUpperMenu();
                var jqxhr = $.post("team_table_result.php?searchCondition=search_all");
                $(".spinner").show();
                jqxhr.success(function(data) {
                    $("#loading_project").html(data);
                    $(".spinner").hide();
                });
                jqxhr.error(function() {
                    //alert("Cannot load page");
                    $().toastmessage('showWarningToast', "Cannot connect server with");
                });
                $("#create_new_team_btn").click(function() {
                    createOrEditStateTeam = "Create";
                    $("#create_edit_panel").show();
                    $("#spinnerCE").show();
                    var jqxhr = $.post("create-edit_form.php?mID=new");
                    jqxhr.success(function(cedata) {
                        $("#loading_ce_form").html(cedata);
                        $("#spinnerCE").hide();
                        $('html,body').animate({scrollTop: $('#create_edit_panel').offset().top}, 'slow');
                    });
                    jqxhr.error(function(result) {
                        $().toastmessage('showWarningToast', "Cannot connect server with: " + result);
                    });
                });
                $("#close_create_panel").click(function() {
                    $("#create_edit_panel").hide();
                    $("#loading_ce_form").empty();
                    $('html,body').animate({scrollTop: $('.navbar').offset().top}, 'slow');
                });
                $("#save_create_panel").click(function() {
                    var teamCode = $("#team_code_form").val();
                    var teamName = $("#team_name_form").val();
                    var teamLeadId = $("#team_lead_form").val();
                    var tSkill = $("#select2_2_form").val();
                    var tType = $("#team_type_form").val();
                    var tManagerID = $("#team_t_manager_form").val();
                    var tRemark = $("#team_remark_in_form").val();
                    if (createOrEditStateTeam == "Edit") {
                        if (tRemark == "" || tRemark == null) {
                            $().toastmessage('showWarningToast', "กรุณาระบุ Remark");
                        } else if ($("#select2_2_form").val() == null) {
                            $().toastmessage('showWarningToast', "กรุณาระบุ ประเภทของบริการ");
                        } else {
                            if (teamLeadId == "" || teamLeadId == null || tManagerID == "" || tManagerID == null) {
                                $().toastmessage('showWarningToast', "กรุณาเลือก Team Leader หรือ Team Manager");
                            } else {
                                if (tSkill == null || tSkill == "") {
                                    tSkill = $("#hideTeamSkill").val();
                                    $(".spinner").show();
                                    var jqxhr = $.post("../model/EditTeamBuilder.php?teamCode=" + teamCode + "&teamName=" + teamName + "&teamLeadId=" + teamLeadId + "&tSkill=" + tSkill + "&tType=" + tType + "&tManagerID=" + tManagerID + "&tRemark=" + tRemark);
                                    jqxhr.success(function(data) {
                                        if (data == 1) {
                                            $("#loading_project").load("team_table_result.php?searchCondition=search_all", function() {
                                                $(".spinner").hide();
                                                $("#create_edit_panel").hide();
                                                $("#loading_ce_form").empty();
                                                updateUpperMenu();
                                                $('html,body').animate({scrollTop: $('#loading_project').offset().top}, 'slow');
                                                createOrEditStateTeam = "Create";
                                            });
                                            $().toastmessage('showSuccessToast', 'แกไขข้อมูลทีมเรียบร้อยแล้ว');
                                        } else {
                                            $().toastmessage('showWarningToast', "ไม่สามารถแก้ไขข้อมูลทีมได้");
                                        }
                                    });
                                } else {

                                    var jqxhr = $.post("../model/EditTeamBuilder.php?teamCode=" + teamCode + "&teamName=" + teamName + "&teamLeadId=" + teamLeadId + "&tSkill=" + tSkill + "&tType=" + tType + "&tManagerID=" + tManagerID + "&tRemark=" + tRemark);
                                    jqxhr.success(function(data) {
                                        if (data == 1) {
                                            //$("#modal-team").modal('hide');
                                            $("#loading_project").load("team_table_result.php?searchCondition=search_all", function() {
                                                $(".spinner").hide();
                                                $("#create_edit_panel").hide();
                                                $("#loading_ce_form").empty();
                                                updateUpperMenu();
                                                $('html,body').animate({scrollTop: $('#loading_project').offset().top}, 'slow');
                                                createOrEditStateTeam = "Create";
                                            });
                                            $().toastmessage('showSuccessToast', 'แกไขข้อมูลทีมเรียบร้อยแล้ว');

                                        } else {
                                            $().toastmessage('showWarningToast', "ไม่สามารถแก้ไขข้อมูลทีมได้");
                                        }
                                    });

                                }
                            }
                        }
                    } else {
                        if (teamLeadId == "" || teamLeadId == null || tManagerID == "" || tManagerID == null) {
                            $().toastmessage('showWarningToast', "กรุณาเลือก Team Leader หรือ Team Manager");
                        } else if ($("#select2_2_form").val() == null) {
                            $().toastmessage('showWarningToast', "กรุณาระบุ ประเภทของบริการ");
                        } else {
                            var jqxhr = $.post("../model/SavingTeamBuilding.php?teamCode=" + teamCode + "&teamName=" + teamName + "&teamLeadId=" + teamLeadId + "&tSkill=" + tSkill + "&tType=" + tType + "&tManagerID=" + tManagerID + "&tRemark=" + tRemark);
                            jqxhr.success(function(data) {
                                if (data == 1) {
                                    //$("#modal-team").modal('hide');
                                    $("#loading_project").load("team_table_result.php?searchCondition=search_all", function() {
                                        $(".spinner").hide();
                                        $("#create_edit_panel").hide();
                                        $("#loading_ce_form").empty();
                                        updateUpperMenu();
                                        $('html,body').animate({scrollTop: $('#loading_project').offset().top}, 'slow');
                                    });
                                    $().toastmessage('showSuccessToast', 'บันทึกข้อมูลทีมเรียบร้อยแล้ว');

                                } else {
                                    $().toastmessage('showWarningToast', "ไม่สามารถบันทึกข้อมูลทีมได้");
                                }
                            });
                        }
                    }
                });
                $("#search_team_btn").click(function() {
                    $("#search_panel").show();
                    $(".spinner").show();
                    var jqxhr = $.post("search-form-page.php");
                    jqxhr.success(function(cedata) {
                        $("#loading_search_form").html(cedata);
                        $(".spinner").hide();
                        $('html,body').animate({scrollTop: $('#search_panel').offset().top}, 'slow');
                    });
                    jqxhr.error(function() {
                        alert("Cannot load page");
                    });
                });
                $("#close_search_panel").click(function() {
                    $("#search_panel").hide();
                    $("#loading_search_form").empty();
                    $(".spinner").show();
                    $("#loading_project").load("team_table_result.php?searchCondition=search_all", function() {
                        $(".spinner").hide();
                        $('html,body').animate({scrollTop: $('#team_tbl_content').offset().top}, 'slow');
                    });
                    //$('html,body').animate({scrollTop: $('.navbar').offset().top}, 'slow');
                });

                $("#logout_click").click(function() {
                    var jqxhr = $.post("../model/LogoutDesSession.php");
                    jqxhr.success(function(data) {
                        alert(data);
                        window.location.assign("../index.php")
                    });
                    jqxhr.error(function() {
                        alert("ไม่สามารถติดต่อกับ Server ได้");
                    });
                });
                $("#search_team_re_btn").click(function() {
                    var teamCode = $("#team_code_search").val();
                    var teamName = $("#team_name_search").val();
                    var teamLead = $("#team_lead_search").val();
                    var tSkill = $("#select2_2").val();
                    var teamType = $("#team_type_search").val();
                    var teamManager = $("#team_t_manager_search").val();
                    var limit = $("#project_limit_search").val();
                    if ($.trim(teamCode).length !== 0 ||
                            $.trim(teamName).length !== 0 ||
                            $.trim(teamLead).length !== 0 ||
                            tSkill !== null ||
                            $.trim(teamType).length !== 0 ||
                            $.trim(teamManager).length !== 0 ||
                            limit != 100) {

                        //window.location.assign("index-builder-search-result.php?searchCondition=condition&teamCode=" + teamCode + "&teamName=" + teamName + "&teamLead=" + teamLead + "&teamSkill=" + tSkill + "&teamType=" + teamType + "&teamManager=" + teamManager);
                        $("#loading_project").load("team_table_result.php?searchCondition=condition&teamCode=" + teamCode + "&teamName=" + teamName + "&teamLead=" + teamLead + "&teamSkill=" + tSkill + "&teamType=" + teamType + "&teamManager=" + teamManager + "&searchLimit=" + limit, function() {
                            //$(".spinner").hide();
                            $('html,body').animate({scrollTop: $('#team_tbl_content').offset().top}, 'slow');
                        });
                    } else {
                        //window.location.assign("index-builder-search-result.php?searchCondition=search_all");
                        $("#loading_project").load("team_table_result.php?searchCondition=search_all", function() {
                            //$(".spinner").hide();
                            $('html,body').animate({scrollTop: $('#team_tbl_content').offset().top}, 'slow');
                        });
                    }
                });

            });
            function deleteTeam(tID, memID) {


                if (confirm("Are you sure?"))
                {
//                    blockPage();
                    var jqxhr = $.post("../model/DeleteTeam.php?t_code=" + tID + "&memID=" + memID);
                    jqxhr.success(function(data) {
                        if (data == 1) {
                            $("#loading_project").load("team_table_result.php?searchCondition=search_all", function() {
                                $(".spinner").hide();
                                $("#create_edit_panel").hide();
                                $("#loading_ce_form").empty();
                                $('html,body').animate({scrollTop: $('#loading_project').offset().top}, 'slow');
                                updateUpperMenu();

                            });
                            $().toastmessage('showSuccessToast', 'ลบข้อมูลทีมเรียบร้อยแล้ว');

                        } else {
                            $().toastmessage('showWarningToast', "ไม่สามารถลบข้อมูลทีมได้");
                        }
                    });
                    jqxhr.error(function(data) {
                        window.location.replace("error.php?error_msg=" + data);
                    });
                }
                else
                {
                    e.preventDefault();
                }

            }
            function clearProjectInsertFields() {
                $("#project_code").val("");
                $("#project_name").val("")
                $("#project_type_select").val("0")
                $("#project_status_select").val("0")
                $("#project_owner_select").val("0")
                $("#project_customer_select").val("0")
                $("#project_manager").val("")
                $("#project_foreman").val("")
                $("#supervisor_control").val("")
                $("#qa_inspectors").val("")
                $("#address_location").val("")
                $("#project_remark").val("")
                $("#team_owner").val("");
            }
            function editTeam(tID, amount) {
                $("#create_edit_panel").show();
                $("#spinnerCE").show();
                var jqxhr = $.post("create-edit_form.php?mID=" + tID);
                jqxhr.success(function(cedata) {
                    $("#loading_ce_form").html(cedata);
                    $("#spinnerCE").hide();
                    $('html,body').animate({scrollTop: $('#create_edit_panel').offset().top}, 'slow');
                });
                jqxhr.error(function(result) {
                    $().toastmessage('showWarningToast', "Cannot connect server with: " + result);
                });
                var millisecondsToWait = 500;
                setTimeout(function() {
                    createOrEditStateTeam = "Edit";
                    var jqxhr = $.post("../model/GetAllTeamForEdit.php?teamID=" + tID);
                    jqxhr.success(function(data) {
                        obj = JSON.parse(data);
                        $("#team_code_form").val(obj.t_code);
                        $("#team_name_form").val(obj.t_Name);
                        $("#team_lead_form").val(obj.t_lead_id);
                        $("#hideTeamSkill").val(obj.tSkill);
                        $("#no_of_member_form").val(amount);
                        $("#team_type_form").val(obj.t_type);
                        $("#team_t_manager_form").val(obj.t_manager_id);
                        $("#team_remark_in_form").val(obj.t_remark);
                    });

                    jqxhr.error(function(data) {
                        window.location.replace("error.php?error_msg=" + data);
                    });
                }, millisecondsToWait);
            }
            function updateUpperMenu() {

                $("#load_all_status").load("../menu-page/load_all_status_page.php", function() {
                    setTimeout(function()
                    {
                        //Update Project
                        $("#update_project_status").load("../menu-page/project_menu_page.php", function() {
                            $(".spinner").hide();
                        });
                        //Update TaskAssignment 
                        $("#update_assign_status").load("../menu-page/taskassign_menu_page.php", function() {
                            $(".spinner").hide();
                        });
                        //Update po inspection 
                        $("#update_poinspection_status").load("../menu-page/poinspection_menu_page.php", function() {
                            $(".spinner").hide();
                        });
                        //Update team member update_team_member_status
                        $("#update_team_member_status").load("../menu-page/teammember_menu_page.php", function() {
                            $(".spinner").hide();
                        });
                        $("#left-menu-load").load("../leftmenu-page/index-left-menu.php");
                    }
                    , 100);
                });

            }
        </script>
    </body>

</html>
